#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/joystick.h>
#include <linux/input.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>



/**
  不需要：  mknod /dev/mycdev002 c  410 0
  
  echo  147  >   /dev/mycdev002
  cat  /dev/mycdev002

*/

#define MY_MAJOR       410
#define MY_MAX_MINORS  1

#define DEVICE_NAME "mycdev002" 

typedef struct {
	struct cdev cdev;
	u8 mem[1024];
} my_device_data;

my_device_data devs[MY_MAX_MINORS];

static struct class *dev_class;

DEFINE_KFIFO(myfifo, char, 1024);

DECLARE_WAIT_QUEUE_HEAD(wq);

static int my_open(struct inode *inode, struct file *filp) {
	my_device_data *my_data;
	pr_info("a3 my_open\n");
	my_data = container_of(inode->i_cdev,  my_device_data, cdev);
	filp->private_data = my_data;
	return 0;
}

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("write");
	ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
	if (ret != 0) {
		pr_err("kfifo_from_user error");
		return 0;
	}
	if (len <= 0)
		return 0;
	*offset += len;
	wake_up(&wq);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *user_buffer,
		size_t count, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("read");
	ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
	if (len <= 0)
		return 0;
	*offset += len;
	return len;
}

unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
	int mask = 0;
	pr_info("my_poll \n");
	poll_wait(flip,&wq,table);
	if(kfifo_is_empty(&myfifo)){

	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

const struct file_operations my_fops = {
		.owner = THIS_MODULE,
		.open = my_open, 
		.read = my_read,
		.write = my_write,
		.poll = my_poll,
		};

static __init int hello_2_init(void) {
	int err;
	int i;
	pr_info("a3 init_module\n");
	err = register_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS,
			"my_device_driver");
	if (err != 0) {
		/* report error */
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MY_MAJOR, i), 1);
		dev_class = class_create(THIS_MODULE, DEVICE_NAME);
		device_create(dev_class, NULL, devs[i].cdev.dev, NULL, DEVICE_NAME);
	}
	return 0;
}

static void __exit hello_2_exit(void) {
	int i;
	pr_info("a3 cleanup_module\n");
	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
	}
	unregister_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS);
}

module_init(hello_2_init);
module_exit(hello_2_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy,81553652@qq.com");
MODULE_DESCRIPTION("A sample driver");
